package com.izhaowo.recommend.util;

import com.jolbox.bonecp.BoneCP;
import com.jolbox.bonecp.BoneCPConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.Connection;

/**
 * Created by HASEE on 2020/6/11.
 */
public class PhoenixConn {

    private static final Logger logger = LoggerFactory.getLogger(PhoenixConn.class);
    private BoneCP pool = null;

    private static final PhoenixConn INSTANCE = new PhoenixConn();

    private PhoenixConn() {
        try {
            Class.forName("org.apache.phoenix.jdbc.PhoenixDriver");
            //  DriverManager.getConnection("jdbc:mysql://192.168.137.130:3306/rzdb?useSSL=false","root","syncdb123!")
            BoneCPConfig config = new BoneCPConfig();
            //      config.setUsername("root")
            //      config.setPassword("syncdb123!")
            config.setJdbcUrl("jdbc:phoenix:hadoop001:2181");
            config.setMinConnectionsPerPartition(2);//最小连接数
            config.setMaxConnectionsPerPartition(5);//最大连接数
            //      config.setCloseConnectionWatch(true) //关闭的时候要不要监控
            config.setCloseConnectionWatch(false); //关闭的时候要不要监控

            pool = new BoneCP(config);
        } catch (Exception e) {
            logger.error("___" + e.getMessage());
        }
    }

    /**
     * 连接
     */
    private Connection getConn() throws Exception {
        if (pool != null) {
            return pool.getConnection();
        }
        return null;
    }

    /**
     * 获取 连接
     */
    public static Connection getConnection() throws Exception {
        return INSTANCE.getConn();
    }


}
